// JavaScript Document
define(function (require, exports, module) {
	var Nav=function(){
	}
	Nav.prototype.init=function(){
		this.element=$("#nav").get(0)
		this.list=new Nav.List(this.element.children[0]);
		
		Nav.clearNullHref(this.element);
		Nav.listItemInnerNum(this.element);
		
		var self=this;
		//a点击
		var aElements=this.element.getElementsByTagName("a");
		for(var i=0;i<aElements.length;i++){
			//alert(aElements[i].href.indexOf("#"))
			if(aElements[i].href.indexOf("#")!=-1){
				aElements[i].removeAttribute("href")
				aElements[i].removeAttribute("target")
			}
			
			if($(aElements[i]).attr("href")!="" && $(aElements[i]).attr("href")){
				$(aElements[i]).bind("click",function(event){self.aElementClickHandle(event)})
			};
		}
		
		$("a",this.element).bind("click",function(event){
			var target=event.currentTarget;
			var parent=$(target).parent();
			var href=$(target).attr("href");
			if(typeof href=="undefined"){
				var className=$(parent).attr("class");
				console.log(className);
				if(className=="focusOpen"){
					$(parent).attr("class","");
				}else{
					$(parent).attr("class","focusOpen");
				}
			}
		})
	}
	Nav.prototype.aElementClickHandle=function(event){
		var element=event.currentTarget;
		var href=element.href;
		this.setCurrentByHref(href);
	}
	
	Nav.prototype.getRootLiIndex=function(liElement){
		var parentParentElement=liElement.parentNode.parentNode;
		var tagName=parentParentElement.tagName;
		if(tagName=="LI"){
			var indexNum=this.getRootLiIndex(parentParentElement);
		}else{
			var indexNum=$(liElement.parentNode).children("li").index(liElement);
		}
		return indexNum;
	}
	
	Nav.prototype.liElementClassReset=function(){
		var aElements=this.element.getElementsByTagName("a");
		var liElement;
		
		for(var i=0;i<aElements.length;i++){
			liElement=aElements[i].parentNode;
			if(liElement.className=="focusOpen current" || liElement.className=="focusOpen"){
				liElement.className="focusOpen";
			}else{
				liElement.className="";
			}
		}
	}
	
	Nav.prototype.setCurrentByHref=function(href){
		
		//对共用一个成功页面的返回按钮做特殊指向处理
		var realHref = href;
		
		/*
		if(realHref.indexOf('/dirfinreqinfo.do?method=dirFinReqQuery') > -1){//直接推送的融资申请信息
			realHref = 'MainMenu.do?nextpage=creditor_ckzjtsdrzxqxx';
		}else if(realHref.indexOf('/finreqinfo.do?method=manageFinReqInfoShow') > -1){//在线确认的融资申请信息
			realHref = 'MainMenu.do?nextpage=creditor_ckzxqrdrzxqxx';
		}
		*/
		
		this.liElementClassReset();
		
		var aElements=this.element.getElementsByTagName("a");
		for(var i=0;i<aElements.length;i++){
			var aElement=aElements[i]
			var liElement=aElement.parentNode;
			
			if(aElement.href.indexOf(realHref) > -1){
				rootLiIndex=this.getRootLiIndex(liElement);
				this.list.focusControl(rootLiIndex);
				
				if(liElement.className=="focusOpen"){
					liElement.className="focusOpen current";
				}else{
					liElement.className="current";
				}
				
			}
		}
	}
	
	
	Nav.clearNullHref=function(selector){
		var aElements=$(selector).get(0).getElementsByTagName("a");
		for(var i=0;i<aElements.length;i++){
			if($(aElements[i]).attr("href")==""){
				$(aElements[i]).removeAttr("href");
			};
		}
	}
	Nav.listItemInnerNum=function(selector){
		var element=$(selector).get(0)
		var list=element.children[0];
		var listItems=list.children
		var total=listItems.length;
		for(var i=0;i<total;i++){
			var spanElement = document.createElement("span"); 
			spanElement.innerHTML=i+1
			listItems[i].appendChild(spanElement); 
		}
	}
	
	
	Nav.cloneMyself=function(selector){
		var listLi=$(selector).children("ul").eq(0).children("li");
		for(var i=0;i<listLi.length;i++){
			var str="navListItem0"+(i+1)
			listLi.eq(i).get(0).id=str;
			if(listLi.eq(i).children("ul").length<=0){
				listLi.eq(i).append($("<ul>").append($("<li>").append(listLi.eq(i).children("a").clone())));
			}
		}
	}
	
	//
	//
	//List
	//
	//
	Nav.List=function(selector){
		this.element=$(selector).get(0)
		this.init();
	}
	Nav.List.prototype.init=function(){
		this.listItems=Array();
		var listItem;
		var childrenElements=this.element.children
		for(var i=0;i<childrenElements.length;i++){
			listItem=new Nav.ListItem(childrenElements[i],i)
			this.listItems.push(listItem);
		}
	}
	Nav.List.prototype.focusControl=function(focusNum){
		for(var i=0;i<this.listItems.length;i++){
			this.listItems[i].focusClose();
		}
		this.listItems[focusNum].focusOpen();
	}
	
	
	//
	//
	//ListItem
	//
	//
	Nav.ListItem=function(selector,num){
		this.element=$(selector).get(0)
		this.num=num;
		this.aElement=this.element.children[0]
		var self=this;
		//$(this.aElement).bind("click",function(event){self.clickHandle(event)})
		this.openState="focusClose"
	}	
	
	Nav.ListItem.prototype.clickHandle=function(event){
		this.focusSwitch();
	}
	Nav.ListItem.prototype.focusSwitch=function(){
		if(this.openState=="focusOpen"){
			this.focusClose();
		}else{
			this.focusOpen();
		}
	}
	Nav.ListItem.prototype.focusOpen=function(){
		return
		this.openState="focusOpen"
		this.element.className="focusOpen"
		if(this.element.children.length>2){
			var height=0;
			height=this.element.children[1].scrollHeight+this.element.children[0].scrollHeight+5
			this.element.style.height=height+'px';
		}
	}
	Nav.ListItem.prototype.focusClose=function(){
		return
		this.openState="focusClose"
		this.element.className="focusClose"
		if(this.element.children.length>2){
			var height=0;
			height=this.element.children[0].scrollHeight+5
			this.element.style.height=height+'px';
		}
	}
	
	module.exports = Nav;
});